# AIDE OpenAI 连接问题修复脚本 (Windows PowerShell)

Write-Host "======================================" -ForegroundColor Blue
Write-Host "AIDE OpenAI 连接问题诊断与修复工具" -ForegroundColor Blue
Write-Host "======================================" -ForegroundColor Blue
Write-Host ""

# 检查 .env.prod 文件
if (-not (Test-Path ".env.prod")) {
    Write-Host "❌ 错误: .env.prod 文件不存在" -ForegroundColor Red
    Write-Host "正在从模板创建..." -ForegroundColor Yellow
    Copy-Item "env-prod-template.txt" ".env.prod"
    Write-Host "✅ 已创建 .env.prod 文件，请编辑其中的配置" -ForegroundColor Green
    Write-Host ""
}

# 检查 OpenAI 配置
Write-Host "🔍 检查 OpenAI 配置..." -ForegroundColor Blue
$envContent = Get-Content ".env.prod" -Raw

$openaiKey = ""
$openaiBaseUrl = ""
$openaiModel = ""

if ($envContent -match "OPENAI_API_KEY=(.+)") {
    $openaiKey = $matches[1].Trim()
}
if ($envContent -match "OPENAI_BASE_URL=(.+)") {
    $openaiBaseUrl = $matches[1].Trim()
}
if ($envContent -match "OPENAI_MODEL=(.+)") {
    $openaiModel = $matches[1].Trim()
}

Write-Host "📋 当前 OpenAI 配置:" -ForegroundColor Cyan
Write-Host "   API Key: $(if ($openaiKey -and $openaiKey -ne 'sk-your_openai_api_key_here') { $openaiKey.Substring(0, [Math]::Min(10, $openaiKey.Length)) + '...' } else { '❌ 未配置' })"
Write-Host "   Base URL: $(if ($openaiBaseUrl) { $openaiBaseUrl } else { '❌ 未配置' })"
Write-Host "   Model: $(if ($openaiModel) { $openaiModel } else { '❌ 未配置' })"
Write-Host ""

# 检查配置是否完整
$needsConfig = $false
if (-not $openaiKey -or $openaiKey -eq "sk-your_openai_api_key_here") {
    Write-Host "⚠️ OPENAI_API_KEY 需要配置" -ForegroundColor Yellow
    $needsConfig = $true
}
if (-not $openaiBaseUrl) {
    Write-Host "⚠️ OPENAI_BASE_URL 需要配置" -ForegroundColor Yellow
    $needsConfig = $true
}

if ($needsConfig) {
    Write-Host ""
    Write-Host "📝 请手动编辑 .env.prod 文件，设置正确的 OpenAI 配置:" -ForegroundColor Yellow
    Write-Host "   notepad .env.prod" -ForegroundColor Gray
    Write-Host ""
    
    $continue = Read-Host "配置完成后，按 Enter 继续测试，或输入 'q' 退出"
    if ($continue -eq 'q') {
        exit 0
    }
}

Write-Host "🔧 修复 Docker 配置..." -ForegroundColor Blue

# 重新构建并启动后端服务
Write-Host "正在重新构建后端服务..." -ForegroundColor Blue
try {
    docker-compose -f docker-compose.prod.yml -f docker-compose.windows.yml --env-file .env.prod up -d --build backend
    Write-Host "✅ 后端服务已重新构建" -ForegroundColor Green
} catch {
    Write-Host "❌ 重新构建失败: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 等待服务启动
Write-Host "等待服务启动..." -ForegroundColor Blue
Start-Sleep -Seconds 15

# 在容器内运行 OpenAI 连接测试
Write-Host "🧪 运行容器内 OpenAI 连接测试..." -ForegroundColor Blue
Write-Host ""

try {
    docker exec aide-backend-prod node test-openai-docker.js
    Write-Host ""
    Write-Host "✅ OpenAI 连接测试完成" -ForegroundColor Green
} catch {
    Write-Host "❌ 容器内测试失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host ""
    Write-Host "🔍 查看后端服务日志:" -ForegroundColor Yellow
    docker logs --tail 50 aide-backend-prod
}

Write-Host ""
Write-Host "======================================" -ForegroundColor Blue
Write-Host "修复完成" -ForegroundColor Blue
Write-Host "======================================" -ForegroundColor Blue

Write-Host ""
Write-Host "💡 如果仍有问题，可能的原因:" -ForegroundColor Yellow
Write-Host "   1. 网络防火墙阻止了容器访问外部 API"
Write-Host "   2. 代理设置需要配置"
Write-Host "   3. OpenAI API 端点不可达"
Write-Host "   4. API Key 无效或权限不足"
Write-Host ""
Write-Host "🔧 进一步排查:" -ForegroundColor Cyan
Write-Host "   查看完整日志: docker logs aide-backend-prod"
Write-Host "   进入容器调试: docker exec -it aide-backend-prod sh"
Write-Host "   测试网络连接: docker exec aide-backend-prod ping ai-api.unidtai.com"
